package edu.umassd.raddacl.steps;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import edu.umassd.raddacl.Cluster;
import edu.umassd.raddacl.Observation;
import edu.umassd.raddacl.calc.ClusterMetrics;
import edu.umassd.raddacl.calc.DataSetMetrics;
import edu.umassd.raddacl.dist.DistanceCalculator;

/**
 * 
 * @author Dan Avila
 * 
 */
@Component
public class ThresholdMetric implements PreclusterThreshold
{
	@Autowired
	private DataSetMetrics dsMetrics;

	@Autowired
	private ClusterMetrics clusterMetrics;

	@Autowired
	private DistanceCalculator<Observation> calc;

	private Double threshold;

	@PostConstruct
	protected void init()
	{
		threshold = Math.abs(dsMetrics.getMean() - dsMetrics.getStandardDev());
	}

	@Override
	public boolean isPrecluster(Cluster cluster)
	{
		if (cluster.getParent() == null)
		{
			return false;
		}

		Observation centroid = clusterMetrics.getCentroid(cluster);
		Observation pCentroid = clusterMetrics.getCentroid(cluster.getParent());

		return calc.distanceFrom(centroid, pCentroid) < threshold;
	}

}